package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.AuthUserMapper;
import cc.rengu.igas.bomp.common.dao.InspectTaskDetailMapper;
import cc.rengu.igas.bomp.common.dao.InspectTaskInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.AuthUserMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.InspectTaskDetailMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.InspectTaskInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.AuthUser;
import cc.rengu.igas.bomp.common.entity.InspectTaskDetail;
import cc.rengu.igas.bomp.common.entity.InspectTaskInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntInspectTaskBean;
import cc.rengu.igas.bomp.facade.request.MchntInspectTaskListQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntInspectTaskListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/03/30
 */
public class MchntInspectTaskListQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntInspectTaskListQueryRequest mchntInspectTaskListQueryRequest = new MchntInspectTaskListQueryRequest();
        ConvertUtil.convertOutput(mchntInspectTaskListQueryRequest);
        return mchntInspectTaskListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntInspectTaskListQueryRequest mchntInspectTaskListQueryRequest = (MchntInspectTaskListQueryRequest) request;
        MchntInspectTaskListQueryResponse mchntInspectTaskListQueryResponse = new MchntInspectTaskListQueryResponse();
        /*获取法人行号信息*/
        String instId = mchntInspectTaskListQueryRequest.getHeader().getInstId();
        /*获取用户信息*/
        String userId = mchntInspectTaskListQueryRequest.getUserId();
        /*获取开始日期*/
        String startDate = mchntInspectTaskListQueryRequest.getStartDate();
        /*获取截止日期*/
        String endDate = mchntInspectTaskListQueryRequest.getEndDate();
        /*获取员工号*/
        String empNo = mchntInspectTaskListQueryRequest.getEmpNo();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*获取分页信息*/
        int pageIndex = mchntInspectTaskListQueryRequest.getPageIndex();
        int pageSize = mchntInspectTaskListQueryRequest.getPageSize();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);

        /*根据用户标识查询用户所属机构*/
        AuthUserMapper authUserMapper = new AuthUserMapperImpl();
        AuthUser authUser = authUserMapper.selectAuthUserById(userId);
        if (null == authUser) {
            rglog.error("查询用户信息失败");
            throw new BizException(RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespCode(), RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespDesc());
        }
        String orgCode = authUser.getOrgCode();

        /*查询巡检任务信息列表*/
        List<MchntInspectTaskBean> mchntInspectTasksList = new ArrayList<>();
        InspectTaskInfoMapper inspectTaskInfoMapper = new InspectTaskInfoMapperImpl();
        PageResult pageResult = inspectTaskInfoMapper.selectBompInspectTaskInfoByInstId(instId, orgCode, startDate, endDate, empNo, pageInfo);
        if (null == pageResult) {
            rglog.error("商户巡检信息查询失败!");
            throw new BizException(RespCodeEnum.MCHNT_INSPECT_INFO_ERROR.getRespCode(), RespCodeEnum.MCHNT_INSPECT_INFO_ERROR.getRespDesc());
        }
        pageInfo = pageResult.getPageInfo();
        List<InspectTaskInfo> inspectTaskInfoList = pageResult.getResult().stream().map(item -> (InspectTaskInfo) item).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(inspectTaskInfoList)) {
            for (InspectTaskInfo inspectTaskInfo : inspectTaskInfoList) {
                /*巡检任务信息赋值*/
                MchntInspectTaskBean mchntInspectTask = new MchntInspectTaskBean();
                /*法人行赋值*/
                mchntInspectTask.setInstId(inspectTaskInfo.getInstId());
                /*巡检任务编号赋值*/
                mchntInspectTask.setInspectTaskNo(inspectTaskInfo.getInspectTaskNo());
                /*巡检任务名称赋值*/
                mchntInspectTask.setInspectTaskDesc(inspectTaskInfo.getInspectTaskDesc());
                /*巡检任务类型赋值*/
                mchntInspectTask.setInspectTaskType(inspectTaskInfo.getInspectTaskType());
                /*巡检方式赋值*/
                mchntInspectTask.setInspectType(inspectTaskInfo.getInspectType());
                /*巡检任务状态赋值*/
                mchntInspectTask.setInspectTaskStatus(inspectTaskInfo.getInspectTaskStatus());
                /*巡检开始时间赋值*/
                mchntInspectTask.setInspectBeginTime(inspectTaskInfo.getInspectBeginTime());
                /*巡检结束时间据赋值*/
                mchntInspectTask.setInspectEndTime(inspectTaskInfo.getInspectEndTime());
                /*创建时间赋值*/
                mchntInspectTask.setCreateTime(inspectTaskInfo.getCreateTime());
                /*根据任务号查询已巡检商户数量*/
                InspectTaskDetailMapper inspectTaskDetailMapper = new InspectTaskDetailMapperImpl();
                InspectTaskDetail inspectTaskDetail = inspectTaskDetailMapper.selectBompInspectTaskDetailCountByTaskNo(instId, inspectTaskInfo.getInspectTaskNo());
                if (null != inspectTaskDetail) {
                    mchntInspectTask.setInspectedMchntNum(inspectTaskDetail.getRemark5());
                }
                /*根据任务号查询w未巡检商户数量*/
                InspectTaskDetail inspectTaskDetailNeed = inspectTaskDetailMapper.selectBompInspectTaskDetailNeedCountByTaskNo(instId, inspectTaskInfo.getInspectTaskNo());
                if (null != inspectTaskDetail) {
                    mchntInspectTask.setNeedInspectMchntNum(inspectTaskDetailNeed.getRemark4());
                }
                mchntInspectTasksList.add(mchntInspectTask);
            }

            mchntInspectTaskListQueryResponse.setMchntInspectTaskList(mchntInspectTasksList);
            mchntInspectTaskListQueryResponse.setTotalPage(pageInfo.getTotalPage());
            mchntInspectTaskListQueryResponse.setPageIndex(pageInfo.getPageIndex());
            mchntInspectTaskListQueryResponse.setPageSize(pageInfo.getPageSize());
            mchntInspectTaskListQueryResponse.setTotalRows(pageInfo.getTotalRows());
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<MchntInspectTaskListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntInspectTaskListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntInspectTaskListQueryResponse mchntInspectTaskListQueryResponse = (MchntInspectTaskListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntInspectTaskListQueryResponse);
    }
}